package com.empire.module.member.controller.admin.presentationpersonrelation;

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import com.empire.framework.common.pojo.PageParam;
import com.empire.framework.common.pojo.PageResult;
import com.empire.framework.common.pojo.CommonResult;
import com.empire.framework.common.util.object.BeanUtils;
import static com.empire.framework.common.pojo.CommonResult.success;

import com.empire.framework.excel.core.util.ExcelUtils;

import com.empire.framework.apilog.core.annotation.ApiAccessLog;
import static com.empire.framework.apilog.core.enums.OperateTypeEnum.*;

import com.empire.module.member.controller.admin.presentationpersonrelation.vo.*;
import com.empire.module.member.dal.dataobject.presentationpersonrelation.PresentationPersonRelationDO;
import com.empire.module.member.service.presentationpersonrelation.PresentationPersonRelationService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 报告-人员关系")
@RestController
@RequestMapping("/member/presentation-person-relation")
@Validated
public class PresentationPersonRelationController {

    @Resource
    private PresentationPersonRelationService presentationPersonRelationService;

    @PostMapping("/create")
    @Operation(summary = "创建报告-人员关系")
//    @PreAuthorize("@ss.hasPermission('member:presentation-person-relation:create')")
    public CommonResult<Integer> createPresentationPersonRelation(@Valid @RequestBody PresentationPersonRelationSaveReqVO createReqVO) {
        return success(presentationPersonRelationService.createPresentationPersonRelation(createReqVO));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报告-人员关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:presentation-person-relation:delete')")
    public CommonResult<Boolean> deletePresentationPersonRelation(@RequestParam("presentationId") Long presentationId,
                                                                  @RequestParam("personId") Long personId) {
        presentationPersonRelationService.deletePresentationPersonRelation(presentationId, personId);
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得报告-人员关系分页")
//    @PreAuthorize("@ss.hasPermission('member:presentation-person-relation:query')")
    public CommonResult<PageResult<PresentationPersonRelationRespVO>> getPresentationPersonRelationPage(@Valid PresentationPersonRelationPageReqVO pageReqVO) {
        PageResult<PresentationPersonRelationDO> pageResult = presentationPersonRelationService.getPresentationPersonRelationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PresentationPersonRelationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报告-人员关系 Excel")
    @PreAuthorize("@ss.hasPermission('member:presentation-person-relation:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPresentationPersonRelationExcel(@Valid PresentationPersonRelationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PresentationPersonRelationDO> list = presentationPersonRelationService.getPresentationPersonRelationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报告-人员关系.xls", "数据", PresentationPersonRelationRespVO.class,
                        BeanUtils.toBean(list, PresentationPersonRelationRespVO.class));
    }

}